package org.ainlolcat.ainscience.UI;

import org.ainlolcat.ainscience.Controller;
import org.ainlolcat.ainscience.project.CircleException;
import org.jdom.JDOMException;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: ain
 * Date: 10/30/11
 * Time: 12:39 AM
 * To change this template use File | Settings | File Templates.
 */
public class AppPanel extends JToolBar{
    public static int APP_PANEL_HEIGHT = 20;
    public static int DEF_BUTT_WID = 70;

    private int endLocation=0;

    public AppPanel(){
        super();
        init();
    }

    public void init(){
        initOpenButton();
        //initSaveButton();
        //initAddTemplateButton();
        //initAddEntityButton();
        //initExitButton();
    }

    public void initOpenButton(){
        JButton openProject = new JButton("Open");
        openProject.setSize(DEF_BUTT_WID,20);
        openProject.setLocation(endLocation, 0);
        endLocation+=DEF_BUTT_WID;
        openProject.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JFileChooser fc = new JFileChooser();
                fc.setFileFilter(new FileFilter() {
                    @Override
                    public boolean accept(File file) {
                        return file.getName().endsWith("sxp")||file.isDirectory();
                    }

                    @Override
                    public String getDescription() {
                        return "AinScience project loader";  //To change body of implemented methods use File | Settings | File Templates.
                    }
                });
                fc.showOpenDialog(null);
                File file = fc.getSelectedFile();
                if (file == null) return;
                try {
                    Controller.getInstance().loadProject(file.getCanonicalPath());
                    MainWindow.getInstance().setTitle(file.getCanonicalPath());
                } catch (JDOMException e1) {
                    JOptionPane.showMessageDialog(null,"Error occured: "+e1.getLocalizedMessage());
                } catch (IOException e1) {
                    JOptionPane.showMessageDialog(null, "Error occured: " + e1.getLocalizedMessage());
                }
            }
        });
        this.add(openProject);
    }

    public void initSaveButton(){
        JButton saveProject = new JButton("Save");
        saveProject.setSize(DEF_BUTT_WID, 20);
        saveProject.setLocation(endLocation, 0);
        endLocation+=DEF_BUTT_WID;
        saveProject.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JFileChooser fc = new JFileChooser();
                fc.setFileFilter(new FileFilter() {
                    @Override
                    public boolean accept(File file) {
                        return file.getName().endsWith("sxp") || file.isDirectory();
                    }

                    @Override
                    public String getDescription() {
                        return "AinScience project saver";  //To change body of implemented methods use File | Settings | File Templates.
                    }
                });
                fc.showSaveDialog(null);
                File file = fc.getSelectedFile();
                try {
                    Controller.getInstance().saveProject(file.getCanonicalPath());
                } catch (Exception e1) {
                    e1.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Error occured: " + e1.getLocalizedMessage());
                }
            }
        });
        this.add(saveProject);
    }

    public void initAddTemplateButton(){
        JButton addTemplate = new JButton("Add Template");
        addTemplate.setSize(DEF_BUTT_WID, 20);
        addTemplate.setLocation(endLocation, 0);
        endLocation+=DEF_BUTT_WID;
        addTemplate.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JFileChooser fc = new JFileChooser();
                fc.setFileFilter(new FileFilter() {
                    @Override
                    public boolean accept(File file) {
                        return file.getName().endsWith("xml") || file.isDirectory();
                    }

                    @Override
                    public String getDescription() {
                        return "AinScience template loader";  //To change body of implemented methods use File | Settings | File Templates.
                    }
                });
                fc.showOpenDialog(null);
                File file = fc.getSelectedFile();
                try {
                    Controller.getInstance().addTemplate(file.getCanonicalPath());
                } catch (JDOMException e1) {
                    JOptionPane.showMessageDialog(null, "Error occured: " + e1.getLocalizedMessage());
                } catch (IOException e1) {
                    JOptionPane.showMessageDialog(null, "Error occured: " + e1.getLocalizedMessage());
                }
            }
        });
        this.add(addTemplate);
    }

    public void initAddEntityButton(){
        JButton addEntity = new JButton("Add Entity");
        addEntity.setSize(DEF_BUTT_WID, 20);
        addEntity.setLocation(endLocation, 0);
        endLocation+=DEF_BUTT_WID;
        addEntity.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(null, "NOT IMPLEMENTED");
            }
        });
        this.add(addEntity);
    }

    public void initExitButton(){
        JButton openProject = new JButton("Exit");
        openProject.setSize(DEF_BUTT_WID,20);
        openProject.setLocation(endLocation, 0);
        endLocation+=DEF_BUTT_WID;
        openProject.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.exit(1);
            }
        });
        this.add(openProject);
    }
}
